package com.gxa.service.Impl;

import com.gxa.common.JsonResult;
import com.gxa.common.R;
import com.gxa.common.State;
import com.gxa.mapper.EmpMapper;
import com.gxa.pojo.Dept;
import com.gxa.pojo.Emp;
import com.gxa.pojo.Params_Son;
import com.gxa.pojo.zk.NewEmp;
import com.gxa.service.EmpService;
import com.gxa.utils.DateUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;
import java.util.List;

@Service
public class EmpServiceImpl implements EmpService {
    @Autowired(required = false)
    private EmpMapper empMapper;

    @Override
    public JsonResult queryAll(Integer page, Integer limit) {
        JsonResult jr= new JsonResult();
        Integer param1 = (page - 1)*limit;
        Integer param2 = limit;
        List<NewEmp> empList = empMapper.queryAll(param1,param2);
        for(NewEmp newEmp :empList){
            newEmp.setEmp_birthday_String(DateUtils.date2String(newEmp.getEmp_birthday(),"yyyy-MM-dd"));
            newEmp.setEmp_joinTime_String(DateUtils.date2String(newEmp.getEmp_joinTime(),"yyyy-MM-dd"));
            newEmp.setEmp_leaveTime_String(DateUtils.date2String(newEmp.getEmp_leaveTime(),"yyyy-MM-dd"));
            newEmp.setEmp_age(DateUtils.compareDate(newEmp.getEmp_birthday_String(),null,2));
            newEmp.setEmp_workAge(DateUtils.compareDate(newEmp.getEmp_joinTime_String(),newEmp.getEmp_leaveTime_String(),2));
        }
        Integer countAll = empMapper.countAll(State.EMP_EMPLOYED);
        jr.setCode(200);
        jr.setMsg("查询成功");
        jr.setCount(countAll);
        jr.setData(empList);
        return jr;
    }

    @Override
    public JsonResult queryWorking(Integer page,Integer limit) {
        JsonResult jr= new JsonResult();
        Integer param1 = (page - 1)*limit;
        Integer param2 = limit;
        List<NewEmp> empList = empMapper.queryWorking(State.EMP_INDUCTION,param1,param2);
        for(NewEmp newEmp :empList){
            newEmp.setEmp_birthday_String(DateUtils.date2String(newEmp.getEmp_birthday(),"yyyy-MM-dd"));
            newEmp.setEmp_joinTime_String(DateUtils.date2String(newEmp.getEmp_joinTime(),"yyyy-MM-dd"));
            newEmp.setEmp_leaveTime_String(DateUtils.date2String(newEmp.getEmp_leaveTime(),"yyyy-MM-dd"));
            newEmp.setEmp_age(DateUtils.compareDate(newEmp.getEmp_birthday_String(),null,2));
            newEmp.setEmp_workAge(DateUtils.compareDate(newEmp.getEmp_joinTime_String(),newEmp.getEmp_leaveTime_String(),2));
        }
        Integer countWorking =empMapper.countWorking(State.EMP_INDUCTION);
        jr.setCode(200);
        jr.setMsg("查询成功");
        jr.setCount(countWorking);
        jr.setData(empList);
        return jr;
    }

    @Override
    public JsonResult queryDeparture(Integer page, Integer limit) {
        JsonResult jr= new JsonResult();
        Integer param1 = (page - 1)*limit;
        Integer param2 = limit;
        List<NewEmp> empList = empMapper.queryDeparture(State.EMP_QUIT,param1,param2);
        for(NewEmp newEmp :empList){
            newEmp.setEmp_birthday_String(DateUtils.date2String(newEmp.getEmp_birthday(),"yyyy-MM-dd"));
            newEmp.setEmp_joinTime_String(DateUtils.date2String(newEmp.getEmp_joinTime(),"yyyy-MM-dd"));
            newEmp.setEmp_leaveTime_String(DateUtils.date2String(newEmp.getEmp_leaveTime(),"yyyy-MM-dd"));
            newEmp.setEmp_age(DateUtils.compareDate(newEmp.getEmp_birthday_String(),null,2));
            newEmp.setEmp_workAge(DateUtils.compareDate(newEmp.getEmp_joinTime_String(),newEmp.getEmp_leaveTime_String(),2));
        }
        Integer countDeparture =empMapper.countDeparture(State.EMP_QUIT);
        jr.setCode(200);
        jr.setMsg("查询成功");
        jr.setCount(countDeparture);
        jr.setData(empList);
        return jr;
    }

    @Override
    public JsonResult queryBlackList(Integer page, Integer limit) {
        JsonResult jr= new JsonResult();
        Integer param1 = (page - 1)*limit;
        Integer param2 = limit;
        List<NewEmp> empList = empMapper.queryBlackList(State.EMP_QUIT_BLACKLIST,param1,param2);
        for(NewEmp newEmp :empList){
            newEmp.setEmp_birthday_String(DateUtils.date2String(newEmp.getEmp_birthday(),"yyyy-MM-dd"));
            newEmp.setEmp_joinTime_String(DateUtils.date2String(newEmp.getEmp_joinTime(),"yyyy-MM-dd"));
            newEmp.setEmp_leaveTime_String(DateUtils.date2String(newEmp.getEmp_leaveTime(),"yyyy-MM-dd"));
            newEmp.setEmp_age(DateUtils.compareDate(newEmp.getEmp_birthday_String(),null,2));
            newEmp.setEmp_workAge(DateUtils.compareDate(newEmp.getEmp_joinTime_String(),newEmp.getEmp_leaveTime_String(),2));
        }
        Integer countBlackList =empMapper.countBlackList(State.EMP_QUIT_BLACKLIST);
        jr.setCode(200);
        jr.setMsg("查询成功");
        jr.setCount(countBlackList);
        jr.setData(empList);
        return jr;
    }

    @Override
    public R addEmp(Emp emp) {
        R r = new R();
        try {
            String number =emp.getEmp_number();
            List<Emp> empList =empMapper.queryEmpNum();
            for (Emp emp1:empList) {
                if(!number.equals(emp1.getEmp_number())){
                   continue;
                }else {
                    r.setCode(400);
                    r.setMsg("该编号已存在,请重新输入编号");
                    return r;
                }
            }
            int result =empMapper.addEmp(emp);
            if(result > 0){
                r.setCode(200);
                r.setMsg("添加员工成功");
            }
        } catch (SQLException e) {
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统出错，请联系管理员");
        }
        return r;
    }

    @Override
    public R importEmp() {
        return null;
    }

    @Override
    public R exportEmp() {
        return null;
    }

    @Transactional
    @Override
    public R editEmp(Emp emp) {
        R r = new R();
        try {
            int result =empMapper.editEmp(emp);
            if(result > 0){
                r.setCode(200);
                r.setMsg("编辑员工成功");
            }
        } catch (SQLException e) {
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统出错，请联系管理员");
        }
        return r;
    }

    @Transactional
    @Override
    public R setWorkingToDeparture(Integer param1,String param2) {
        R r = new R();
        try {
            int result = empMapper.setWorkingToDeparture(param1,param2);
            if(result > 0){
                r.setCode(200);
                r.setMsg("离职成功");
            }
        } catch (SQLException e) {
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差，请联系管理员！");
        }
        return r;
    }

    @Transactional
    @Override
    public R setDepartureToBlackList(Integer param1,String param2) {
        R r = new R();
        try {
            int result = empMapper.setDepartureToBlackList(param1,param2);
            if(result > 0){
                r.setCode(200);
                r.setMsg("拉黑成功");
            }
        } catch (SQLException e) {
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差，请联系管理员！");
        }
        return r;
    }

    @Transactional
    @Override
    public R setBlackListToDeparture(Integer param1,String param2) {
        R r = new R();
        try {
            int result = empMapper.setBlackListToDeparture(param1,param2);
            if(result > 0){
                r.setCode(200);
                r.setMsg("取消拉黑成功");
            }
        } catch (SQLException e) {
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差，请联系管理员！");
        }
        return r;
    }

    @Override
    public R queryEmpDetails(String emp_number) {
        R r = new R();
        List<NewEmp> empList = empMapper.queryEmpDetails(emp_number);
        for(NewEmp newEmp :empList){
            newEmp.setEmp_birthday_String(DateUtils.date2String(newEmp.getEmp_birthday(),"yyyy-MM-dd"));
            newEmp.setEmp_joinTime_String(DateUtils.date2String(newEmp.getEmp_joinTime(),"yyyy-MM-dd"));
            newEmp.setEmp_leaveTime_String(DateUtils.date2String(newEmp.getEmp_leaveTime(),"yyyy-MM-dd"));
            newEmp.setEmp_age(DateUtils.compareDate(newEmp.getEmp_birthday_String(),null,2));
            newEmp.setEmp_workAge(DateUtils.compareDate(newEmp.getEmp_joinTime_String(),newEmp.getEmp_leaveTime_String(),2));
        }
        r.setCode(200);
        r.setMsg("查询成功");
        r.setData(empList);
        return r;
    }

    @Override
    public JsonResult fuzzyQuery(@Param("keyWord") String keyWord,Integer page,Integer limit) {
        JsonResult jr =new JsonResult();
        if(keyWord != null){
            Integer param1 = (page - 1)*limit;
            Integer param2 = limit;
            List<NewEmp> empList = empMapper.fuzzyQuery(keyWord,param1,param2,State.EMP_EMPLOYED);
            for(NewEmp newEmp :empList){
                newEmp.setEmp_birthday_String(DateUtils.date2String(newEmp.getEmp_birthday(),"yyyy-MM-dd"));
                newEmp.setEmp_joinTime_String(DateUtils.date2String(newEmp.getEmp_joinTime(),"yyyy-MM-dd"));
                newEmp.setEmp_leaveTime_String(DateUtils.date2String(newEmp.getEmp_leaveTime(),"yyyy-MM-dd"));
                newEmp.setEmp_age(DateUtils.compareDate(newEmp.getEmp_birthday_String(),null,2));
                newEmp.setEmp_workAge(DateUtils.compareDate(newEmp.getEmp_joinTime_String(),newEmp.getEmp_leaveTime_String(),2));
            }
            Integer countfuzzyQuery =empMapper.countfuzzyQuery(keyWord,State.EMP_EMPLOYED);
            jr.setCode(200);
            jr.setMsg("查询成功");
            jr.setCount(countfuzzyQuery);
            jr.setData(empList);
            return jr;
        }
        return null;
    }

    @Override
    public R queryEdu() {
        Integer param_id = State.PARAMS_DELETE;
        R r = new R();
        List<Params_Son> list = empMapper.queryEdu(param_id);
        r.setCode(200);
        r.setMsg("查询成功");
        r.setData(list);
        return r;
    }

    @Override
    public R queryDept() {
        R r = new R();
        List<Dept> list = empMapper.queryDept();
        r.setCode(200);
        r.setMsg("查询成功");
        r.setData(list);
        return r;
    }

    @Override
    public R queryTitle() {
        Integer param_id = State.PARAMS_TITLE;
        R r = new R();
        List<Params_Son> list = empMapper.queryTitle(param_id);
        r.setCode(200);
        r.setMsg("查询成功");
        r.setData(list);
        return r;
    }

    @Override
    public R queryJob() {
        Integer param_id = State.PARAMS_POST;
        R r = new R();
        List<Params_Son> list = empMapper.queryJob(param_id);
        r.setCode(200);
        r.setMsg("查询成功");
        r.setData(list);
        return r;
    }

    @Override
    public R queryCategory() {
        Integer param_id = State.PARAMS_CATEGORY;
        R r = new R();
        List<Params_Son> list = empMapper.queryCategory(param_id);
        r.setCode(200);
        r.setMsg("查询成功");
        r.setData(list);
        return r;
    }

    @Override
    public R queryNation() {
        Integer param_id = State.PARAMS_NATION;
        R r = new R();
        List<Params_Son> list = empMapper.queryNation(param_id);
        r.setCode(200);
        r.setMsg("查询成功");
        r.setData(list);
        return r;
    }

    @Override
    public R queryBloodType() {
        Integer param_id = State.PARAMS_BLOOD;
        R r = new R();
        List<Params_Son> list = empMapper.queryBloodType(param_id);
        r.setCode(200);
        r.setMsg("查询成功");
        r.setData(list);
        return r;
    }

    @Override
    public R queryMaritalSta() {
        Integer param_id = State.PARAMS_MARRIAGE;
        R r = new R();
        List<Params_Son> list = empMapper.queryMaritalSta(param_id);
        r.setCode(200);
        r.setMsg("查询成功");
        r.setData(list);
        return r;
    }

    @Override
    public JsonResult fuzzyQueryWorking(String keyWord, Integer page, Integer limit) {
        JsonResult jr =new JsonResult();
        if(keyWord != null){
            Integer param1 = (page - 1)*limit;
            Integer param2 = limit;
            List<NewEmp> empList = empMapper.fuzzyQueryWorking(keyWord,param1,param2,State.EMP_INDUCTION);
            for(NewEmp newEmp :empList){
                newEmp.setEmp_birthday_String(DateUtils.date2String(newEmp.getEmp_birthday(),"yyyy-MM-dd"));
                newEmp.setEmp_joinTime_String(DateUtils.date2String(newEmp.getEmp_joinTime(),"yyyy-MM-dd"));
                newEmp.setEmp_leaveTime_String(DateUtils.date2String(newEmp.getEmp_leaveTime(),"yyyy-MM-dd"));
                newEmp.setEmp_age(DateUtils.compareDate(newEmp.getEmp_birthday_String(),null,2));
                newEmp.setEmp_workAge(DateUtils.compareDate(newEmp.getEmp_joinTime_String(),newEmp.getEmp_leaveTime_String(),2));
            }
            Integer countfuzzyQuery =empMapper.countfuzzyQueryWorking(keyWord,State.EMP_INDUCTION);
            jr.setCode(200);
            jr.setMsg("查询成功");
            jr.setCount(countfuzzyQuery);
            jr.setData(empList);
            return jr;
        }
        return null;
    }

    @Override
    public JsonResult fuzzyQueryDep(String keyWord, Integer page, Integer limit) {
        JsonResult jr =new JsonResult();
        if(keyWord != null){
            Integer param1 = (page - 1)*limit;
            Integer param2 = limit;
            List<NewEmp> empList = empMapper.fuzzyQueryDep(keyWord,param1,param2,State.EMP_QUIT);
            for(NewEmp newEmp :empList){
                newEmp.setEmp_birthday_String(DateUtils.date2String(newEmp.getEmp_birthday(),"yyyy-MM-dd"));
                newEmp.setEmp_joinTime_String(DateUtils.date2String(newEmp.getEmp_joinTime(),"yyyy-MM-dd"));
                newEmp.setEmp_leaveTime_String(DateUtils.date2String(newEmp.getEmp_leaveTime(),"yyyy-MM-dd"));
                newEmp.setEmp_age(DateUtils.compareDate(newEmp.getEmp_birthday_String(),null,2));
                newEmp.setEmp_workAge(DateUtils.compareDate(newEmp.getEmp_joinTime_String(),newEmp.getEmp_leaveTime_String(),2));
            }
            Integer countfuzzyQuery =empMapper.countfuzzyQueryDep(keyWord,State.EMP_QUIT);
            jr.setCode(200);
            jr.setMsg("查询成功");
            jr.setCount(countfuzzyQuery);
            jr.setData(empList);
            return jr;
        }
        return null;
    }

    @Override
    public JsonResult fuzzyQueryBlackList(String keyWord, Integer page, Integer limit) {
        JsonResult jr =new JsonResult();
        if(keyWord != null){
            Integer param1 = (page - 1)*limit;
            Integer param2 = limit;
            List<NewEmp> empList = empMapper.fuzzyQueryBlackList(keyWord,param1,param2,State.EMP_QUIT_BLACKLIST);
            for(NewEmp newEmp :empList){
                newEmp.setEmp_birthday_String(DateUtils.date2String(newEmp.getEmp_birthday(),"yyyy-MM-dd"));
                newEmp.setEmp_joinTime_String(DateUtils.date2String(newEmp.getEmp_joinTime(),"yyyy-MM-dd"));
                newEmp.setEmp_leaveTime_String(DateUtils.date2String(newEmp.getEmp_leaveTime(),"yyyy-MM-dd"));
                newEmp.setEmp_age(DateUtils.compareDate(newEmp.getEmp_birthday_String(),null,2));
                newEmp.setEmp_workAge(DateUtils.compareDate(newEmp.getEmp_joinTime_String(),newEmp.getEmp_leaveTime_String(),2));
            }
            Integer countfuzzyQuery =empMapper.countfuzzyQueryBlackList(keyWord,State.EMP_QUIT_BLACKLIST);
            jr.setCode(200);
            jr.setMsg("查询成功");
            jr.setCount(countfuzzyQuery);
            jr.setData(empList);
            return jr;
        }
        return null;
    }

}
